/**
 *
 * @author AD_DA
 */
package BL;

import Bean.BArticle;
import Util.DBConnection;
import java.io.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;


public class BLArticle {
    public static List<BArticle> getBaiVietMoi() throws SQLException, FileNotFoundException, IOException{
        List<BArticle> DSBaiViet =new ArrayList<BArticle>();
        
        
        Connection conn = DBConnection.getConnection();
        if (conn != null) {
            Statement stm = (Statement) conn.createStatement();
            String sql = "SELECT * FROM  baiviet WHERE 1 ORDER BY NgayViet DESC LIMIT 0 ,5";
            ResultSet rs = stm.executeQuery(sql);
            int i=0;
            while (rs.next()) {
                BArticle BV = new BArticle();
                BV.setMaBaiViet(rs.getInt("Mabaiviet"));
                BV.setTieuDe(rs.getString("tieude"));
                BV.setGioiThieuBaiViet(rs.getString("GioiThieuBaiViet"));
                BV.setHinhAnhDD("D:\\MaporBV"+(i)+".png");
                File image = new File("D:\\MaporBV"+(i++)+".png");
                FileOutputStream fos = new FileOutputStream(image);
                byte[] buffer = new byte[1];
                InputStream is = rs.getBinaryStream("HinhAnhDD");
                while (is.read(buffer) > 0) {
                fos.write(buffer);
                }
                BV.setNgayViet(rs.getDate("NgayViet"));
                BV.setNoiDung(rs.getString("NoiDung"));
                BV.setTag(rs.getInt("Tag"));
                BV.setSoLuongXem(rs.getInt("SoLuongXem"));
                DSBaiViet.add(BV);                
            }            
        }
        conn.close();
        return DSBaiViet;
    }
   public  void main(String[] args) throws FileNotFoundException, IOException {
        try {
            List<BArticle> BC= new ArrayList<BArticle>();
                    BC = getBaiVietMoi();
            for(int i=0;i<BC.size();i++)
            {
                System.out.print(BC.get(i).getTieuDe());
            }
        } catch (SQLException ex) {
            //Logger.getLogger(DBHelper.class.getName()).log(Level.SEVERE, null, ex);
            ex.printStackTrace();
        
    }
    }
}
